<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function swap(arr, i, j) {
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
      function BubbleSort(arr) {
        if (arr.length === 0) {
          return [];
        }

        let len = arr.length;
        let lastChangeBorder = len - 1;
        for (let i = 0; i < len - 1; i++) {
          let isSorted = true;
          for (let j = 0; j < lastChangeBorder - i; j++) {
            if (arr[j] > arr[j + 1]) {
              swap(arr, j, j + 1);
              isSorted = false;
              lastChangeBorder = j;
            }
          }
          
          if (!isSorted) {
            break;
          }
        }
        return arr;
      }
    </script>
  </body>
</html>
